/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.proyecto;

import com.proyecto.datos.EntityCatedratico;
import com.proyecto.modelado.Catedratico;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mario
 */
public class frmModificarCatedratico extends javax.swing.JInternalFrame {
    
    Catedratico cate;

      public void PopularTabla() {

        //POPULANDO TABLE 10

        DefaultTableModel tabla1;
        Object[] fila = new Object[4];
        try {
            String[] columName = {"ID", "Nombres","Apellidos"};
            tabla1 = new DefaultTableModel(null, columName);
            //populamos el arraylist de la tabla
            ArrayList arry_10 = new ArrayList();
            cate = new Catedratico();
            EntityCatedratico entCate = new EntityCatedratico();

            arry_10 = cate.MostrarCatedraticos();
            if (arry_10 != null) {
                for (int i = 0; i < arry_10.size(); i++) {
                    entCate = (EntityCatedratico) arry_10.get(i);
                    fila[0] = entCate.getId();
                    fila[1] = entCate.getNombre();
                    fila[2] = entCate.getApellido();
                    

                    tabla1.addRow(fila);
                }




                this.jtbleCatedratico.setModel(tabla1);
            } else {
                JOptionPane.showMessageDialog(rootPane, "No se recuperaron datos de la base de datos", "CONFIRMACIÓN", JOptionPane.INFORMATION_MESSAGE);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex.toString());
        }
    }
     
     
     public void BloquearControles(){
         this.jtxtCodAsig.setEnabled(false);
         this.jtxtID.setEnabled(false);
         this.jtxtNombreMate.setEnabled(false);
     }
     
     public void Habilitarcontroles(){
         this.jtxtCodAsig.setEnabled(true);
         this.jtxtID.setEnabled(true);
         this.jtxtNombreMate.setEnabled(true);
     }
    
    
    
    public frmModificarCatedratico() {
        initComponents();
        
        PopularTabla();
        
        this.setTitle("Modificar/Eliminar catedratico");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbleCatedratico = new javax.swing.JTable();
        jbtnCancelar = new javax.swing.JButton();
        jbtnCargar = new javax.swing.JButton();
        jtxtID = new javax.swing.JTextField();
        jtxtCodAsig = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jbtnEliminar = new javax.swing.JButton();
        jbtnActualizar = new javax.swing.JButton();
        jtxtNombreMate = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);

        jtbleCatedratico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtbleCatedratico.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jScrollPane1.setViewportView(jtbleCatedratico);

        jbtnCancelar.setText("Cancelar");
        jbtnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCancelarMouseClicked(evt);
            }
        });
        jbtnCancelar.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jbtnCancelarItemStateChanged(evt);
            }
        });

        jbtnCargar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/cargar.png"))); // NOI18N
        jbtnCargar.setText("Cargar");
        jbtnCargar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnCargarMouseClicked(evt);
            }
        });
        jbtnCargar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCargarActionPerformed(evt);
            }
        });

        jtxtID.setText("ID");

        jtxtCodAsig.setText("Nombre");

        jLabel3.setText("Apellido:");

        jLabel2.setText("Nombre:");

        jLabel1.setText("ID:");

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/images (2).jpg"))); // NOI18N
        jbtnEliminar.setText("Eliminar");
        jbtnEliminar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnEliminarMouseClicked(evt);
            }
        });

        jbtnActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/proyecto/imagenes/editar.png"))); // NOI18N
        jbtnActualizar.setText("Actualizar");
        jbtnActualizar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtnActualizarMouseClicked(evt);
            }
        });

        jtxtNombreMate.setText("Apellido");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(jbtnCargar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
                        .addComponent(jbtnActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(77, 77, 77)
                        .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(68, 68, 68))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtnCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(jLabel2)
                        .addGap(3, 3, 3)
                        .addComponent(jtxtCodAsig)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addGap(3, 3, 3)
                        .addComponent(jtxtNombreMate, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtCodAsig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtNombreMate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(66, 66, 66)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnActualizar)
                    .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnCargar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbtnCancelar)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnCancelarItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jbtnCancelarItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnCancelarItemStateChanged

    private void jbtnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCancelarMouseClicked

        this.dispose();
    }//GEN-LAST:event_jbtnCancelarMouseClicked

    private void jbtnCargarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnCargarMouseClicked
        Habilitarcontroles();

        int fila=this.jtbleCatedratico.getSelectedRow();

        this.jtxtID.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 0)));
        this.jtxtCodAsig.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 1)));
        this.jtxtNombreMate.setText(String.valueOf(this.jtbleCatedratico.getValueAt(fila, 2)));

    }//GEN-LAST:event_jbtnCargarMouseClicked

    private void jbtnCargarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCargarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnCargarActionPerformed

    private void jbtnEliminarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnEliminarMouseClicked
        try{
            int id_catedratico=Integer.parseInt(this.jtxtID.getText());

            cate = new Catedratico();
            String msj=cate.EliminarCatedratico(id_catedratico);

            JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",JOptionPane.INFORMATION_MESSAGE);

            PopularTabla();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jbtnEliminarMouseClicked

    private void jbtnActualizarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtnActualizarMouseClicked

        try{
            int id_catedratico=Integer.parseInt(this.jtxtID.getText());
            String nombre=this.jtxtCodAsig.getText();
            String apellido=this.jtxtNombreMate.getText();

            cate = new Catedratico();
            String msj=cate.ModificarCatedratico(id_catedratico, nombre, apellido);

            JOptionPane.showMessageDialog(rootPane, msj,"Confirmacion",JOptionPane.INFORMATION_MESSAGE);

            PopularTabla();

        }catch(Exception ex){
            JOptionPane.showMessageDialog(rootPane, ex.toString(),"Error",JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_jbtnActualizarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnActualizar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnCargar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JTable jtbleCatedratico;
    private javax.swing.JTextField jtxtCodAsig;
    private javax.swing.JTextField jtxtID;
    private javax.swing.JTextField jtxtNombreMate;
    // End of variables declaration//GEN-END:variables
}
